function jcb_write_lumerical_profile(filename,fspfile,type,x,y,z,f,fpoint,jcb_profile,dim) 
    if nargin<10
        dim='';
    end
    
    hash = [length(x) length(y) length(z)]>1;
    if sum(hash)~=2
        error('unexpected input profile axis sizes');
    end
    
    % find lambda
    lambda=1e9*jcb_c/f(fpoint);
    
    axisnames='xyz';
    axislist={x(:),y(:),z(:)};
    one=find(hash,1,'first');
    two=find(hash,1,'last');
    idle=find(~hash);
    
    profile = squeeze(jcb_profile(:,:,:,fpoint));
    
    fileid=fopen(filename,'w');
    try
        if fileid<0
            warning(['couldn''t open file ' filename]);
            return
        end
        % note double %% tags to get an actual % from sprintf
        if strcmpi(type,'nearfield')
            if strcmpi(dim,'x')
                component='Ex';
            elseif strcmpi(dim,'y')
                component='Ey';
            elseif strcmpi(dim,'z')
                component='Ez';
            else
                component='|E|^2';
            end
            fprintf(fileid,'%% Near-field %s profile at lambda=',component);
            topdatspec = ' %- 22g';
            datspec    = ' %- 22.20g';
        elseif strcmpi(type,'index')
            fprintf(fileid,'%% Refractive index profile at lambda=');
            topdatspec = ' %- 7g';
            datspec    = ' %- 7.3g';
        else
            error(['unrecognised profile type ''' type ''' used']);
        end
        fprintf(fileid,'%4f nm for %s.fsp\n',lambda,fspfile);
        fprintf(fileid,'%% cross section taken at %s=%gnm\n',axisnames(idle),axislist{idle});
        fprintf(fileid,'%% data format:\n');
        fprintf(fileid,       strrep('%% lambda     y1               y2                y3       ...\n','y',axisnames(two)));
        if strcmpi(type,'nearfield')
            fprintf(fileid,strrep(strrep('%%   x1   |E(x1,y1)|^2    |E(x1,y2)|^2      |E(x1,y3)|^2  ...\n','x',axisnames(one)),'y',axisnames(two)));
            fprintf(fileid,strrep(strrep('%%   x2   |E(x2,y1)|^2    |E(x2,y2)|^2      |E(x2,y3)|^2  ...\n','x',axisnames(one)),'y',axisnames(two)));
            fprintf(fileid,strrep(strrep('%%   x3   |E(x3,y1)|^2    |E(x3,y2)|^2      |E(x3,y3)|^2  ...\n','x',axisnames(one)),'y',axisnames(two)));
        else
            fprintf(fileid,strrep(strrep('%%   x1   n(x1,y1)    (x1,y2)      (x1,y3)  ...\n','x',axisnames(one)),'y',axisnames(two)));
            fprintf(fileid,strrep(strrep('%%   x2   n(x2,y1)    (x2,y2)      (x2,y3)  ...\n','x',axisnames(one)),'y',axisnames(two)));
            fprintf(fileid,strrep(strrep('%%   x3   n(x3,y1)    (x3,y2)      (x3,y3)  ...\n','x',axisnames(one)),'y',axisnames(two)));   
        end
        fprintf(fileid,'%%   ...      ...             ...               ...       ...\n\n');
        outputnums = [lambda axislist{two}'; axislist{one} profile];

        linespec='\n';
        for ii=1:size(outputnums,2)-1
            linespec = [topdatspec linespec];
        end
        linespec = [' %- 10g' linespec];
        fprintf(fileid,linespec,outputnums(1,:));


        linespec='\n';
        for ii=1:size(outputnums,2)-1
            linespec = [datspec linespec];
        end
        linespec = [' %- 10g' linespec];

        % need to transpose in order to get the correct orientation - it loops
        % through dim1 first.
        fprintf(fileid,linespec,outputnums(2:end,:)');
    catch E
        fclose(fileid);
        throw(E);
    end
    fclose(fileid);
    
end